using System;
using System.Threading;
using CommunityToolkit.Mvvm.ComponentModel;

namespace AvaloniaApplication1.Component.ViewModel;

public partial class InfoViewModel:ObservableObject,IDisposable
{
    /// <summary>
    /// 显示标题
    /// </summary>
    [ObservableProperty] private string _title = "Info";
    /// <summary>
    /// 是否不透明
    /// </summary>
    [ObservableProperty] private bool _isOpaque = false;
    /// <summary>
    /// 是否可关闭
    /// </summary>
    [ObservableProperty] private bool _isClosable = true;
    /// <summary>
    /// 是否打开
    /// </summary>
    [ObservableProperty] private bool _isOpen = false;
    /// <summary>
    /// 严重性
    /// </summary>
    [ObservableProperty] private string _severity = "Warning";
    /// <summary>
    /// 消息
    /// </summary>
    [ObservableProperty] private string _message = "Hello World!";
    // 添加定时器用于自动隐藏
    // 使用正确的 Timer 类型
    private Timer _autoHideTimer;
        
    public void ShowInfo(string message, string severity = "Info", string title = "Info",  int delay = 10000)
    {
        // 停止旧的定时器
        if (_autoHideTimer!= null)
        {
            _autoHideTimer.DisposeAsync();
        }
       
        Message = message;
        Severity = severity;
        Title = title;
        IsOpen = true;
        // 正确创建和启动定时器
        _autoHideTimer = new Timer(HideInfo, null, delay, Timeout.Infinite);
    }
        
    public void HideInfo(object? state)
    {
        IsOpen = false;
    }

    public void Dispose()
    {
        // 清理定时器
        _autoHideTimer.DisposeAsync();
    }
}